<template>
  <div>
    <TopFilter ref="formPanel" :list="topFilterList" :cols="3" :labelWidth="90" @filterChange="searchSalesPart"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartOutboundPartSalesQuoteComponentsSearchSalesOrder"
      :fetchapi="fetchApi"
      height="auto"
      maxHeight="350"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      selectionType="single"
    ></FilterTable>

    <div class="confirm-button-style">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { searchSalesPart, isLocked } from '@/api/part/partOut/partSales/index';
import { getUserId } from '@/assets/js/auth';
import { findByEmployeeId } from '@/api/common/auth/employeeAuth/index';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'salesOrder',
  mixins: [dictionary],
  props: ['salesNo'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: searchSalesPart, // 调用接口查询工单信息
      fetchParams: { xhrAbort: true }, // 调用数据
      columns: this.createTablelist(),
      selectedRows: [],
      addBtnState: null,
      isFinishStatus: 10041002,
      lsit: [],
      serviceAdvisorData: []
    };
  },
  mounted() {
    this.getQueryBookingInformation();
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.lsit = [];
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '配件销售单号',
          fieldName: 'salesNo',
          initialValue: this.salesNo
        },
        {
          type: 'SELECT',
          label: '是否出库',
          fieldName: 'isFinish',
          initialValue: 10041002,
          itemList: this.createDictList(1004)
        }
      ];
    },

    createTablelist() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '配件销售单号',
          dataIndex: 'SALES_PART_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单据号',
          dataIndex: 'RO_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户代码',
          dataIndex: 'CUSTOMER_CODE',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'CUSTOMER_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售材料费',
          dataIndex: 'SALES_PART_AMOUNT',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          sorter: true,
          render: props => {
            return <span>{this.getEmployeeName(props.row.LOCK_USER)}</span>;
          },
          showOverflowTooltip: true,
          filter: true,
          filterType: 'checkBox',
          filterItems: this.serviceAdvisorData
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },

    searchSalesPart(val) {
      const params = Object.assign({}, val, { xhrAbort: false });
      this.fetchParams = params;
      if (params.isFinish != null) {
        this.isFinishStatus = params.isFinish;
      }
    },
    async getQueryBookingInformation() {
      let res = await getAllUser();
      this.serviceAdvisorData = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
    },
    getEmployeeName(userId) {
      const item = this.serviceAdvisorData.find(x => {
        return x.value == userId;
      });
      return item ? item.text : '';
    },
    // 选中确定后关闭窗口
    async saveDialog() {
      if (this.onChangeColumn.length) {
        let obj = this.onChangeColumn[0];
        const soNO = obj.SALES_PART_NO;
        let lock = await getUserId();
        if (obj.LOCK_USER != null && obj.LOCK_USER != '' && lock != obj.LOCK_USER) {
          // 有锁定人弹出提示
          const item = this.serviceAdvisorData.find(x => {
            return x.value == obj.LOCK_USER;
          });
          return this.$notify.warning({ title: '提示信息', message: '该单号已被' + item.text + '锁定' });
        } else {
          this.$emit('confirm', obj);
        }
      }
    },
    // 点击取消关闭窗口
    closeDialog() {
      this.$emit('cancel', false);
    }
    // 销售单号查询
  }
};
</script>
<style lang="less" scoped>
.confirm-button-style {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 9;
  border-top: 1px solid #e9e9e9;
  padding: 10px 20px;
  background: #fff;
  text-align: right;
}
</style>
